package top.itmeans.yes.portal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.itmeans.yes.common.api.CommonResult;
import top.itmeans.yes.entity.MemberReceiveAddress;
import top.itmeans.yes.portal.service.MemberReceiveAddressService;

import java.util.List;

/**
 * 会员收货地址管理Controller
 * @author Yiming.Cao
 */
@Api(tags = "MemberReceiveAddressController", description = "会员收货地址管理")
@RequestMapping("/member/address")
@RestController
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    @ApiOperation("添加收货地址")
    @PostMapping
    public CommonResult add(@RequestBody MemberReceiveAddress address) {
        int count = memberReceiveAddressService.add(address);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除收货地址")
    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable Long id) {
        int count = memberReceiveAddressService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改收货地址")
    @PostMapping("/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody MemberReceiveAddress address) {
        int count = memberReceiveAddressService.update(id, address);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("显示所有收货地址")
    @GetMapping("/list")
    public CommonResult<List<MemberReceiveAddress>> list() {
        List<MemberReceiveAddress> addressList = memberReceiveAddressService.list();
        return CommonResult.success(addressList);
    }

    @ApiOperation("获取收货地址详情")
    @GetMapping("/{id}")
    public CommonResult<MemberReceiveAddress> getItem(@PathVariable Long id) {
        MemberReceiveAddress address = memberReceiveAddressService.getItem(id);
        return CommonResult.success(address);
    }
}
